'use strict';

/**
 * @ngdoc function
 * @name webApp.controller:TextcommunicateCtrl
 * @description
 * # TextcommunicateCtrl
 * Controller of the webApp
 */


angular.module('webApp')
  .controller('textCommunicate', function ($scope,$rootScope,promiseTextCommunicate,$location,cookie) {
    $scope.$on('$stateChangeSuccess',function(){
      if($location.$$path == '/root/userCommunicate'){
        $scope.priseHand = false
      }
      else{
        $scope.priseHand = true
      }
      $scope.lists = [];
      $scope.extras = [];
      promiseTextCommunicate.getComments().then(function(data){
        $scope.lists = data.lists;
        $scope.extras = data.extras;
      });
    });
    $rootScope.rightButClick = function(){
      if($location.$$path == '/root/textCommunicate'){
        $rootScope.$broadcast('footShow',{title: '写评论',action: 'show'});
      }
    };
    //监听评论消息
    $rootScope.$on('/root/textCommunicate',function(ele,data){
      if(data.action == '写评论'){
        $scope.lists.splice(0,0,data.data);
      }
      else if(data.action == '写回复'){
        for(var i = 0;i < $scope.lists.length;i++){
          if($scope.lists[i].id == cookie.getCookie('replayId')){
            if($scope.lists[i].replies){
              $scope.lists[i].replies.splice(0,0,data.data);
            }
            else{
              $scope.lists[i].replies = [data.data];
            }
          }
        }
        for(var i = 0;i < $scope.extras.length;i++){
          if($scope.extras[i].id == cookie.getCookie('replayId')){
            if($scope.extras[i].replies){
              $scope.extras[i].replies.splice(0,0,data.data);
            }
            else{
              $scope.extras[i].replies = [data.data];
            }
          }
        }
      }
    });
});
